<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" dir="ltr" class="client-nojs">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>std::async - cppreference.com</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">


<link rel="shortcut icon" href="../../../favicon.ico">



<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-css:7:472787eddcf4605d11de8c7ef047234f */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/thread/async","wgTitle":"cpp/thread/async","wgCurRevisionId":69576,"wgArticleId":2516,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"en","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgMonthNamesShort":["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"wgRelevantPageName":"cpp/thread/async","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"en","language":"en","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-js:7:ca03345b1e2c4d90a25d968753a73b92 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style>
<style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style>
<!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]-->
</head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_thread_async skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        
        <!-- /header -->
        <!-- content -->
        <div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
                <h1 id="firstHeading" class="firstHeading">
<span style="font-size:0.7em; line-height:130%">std::</span>async</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">From cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../thread.html" title="cpp/thread">thread</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr">
<div class="t-navbar" style="">
<div class="t-navbar-sep"> </div>
<div class="t-navbar-head">
<a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div>
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.1.html" title="cpp/language"> Language</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header"> Standard library headers</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concept.html" title="cpp/concept"> Concepts</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility"> Utilities library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string"> Strings library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container"> Containers library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm"> Algorithms library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator"> Iterators library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric"> Numerics library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io"> Input/output library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale"> Localizations library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex"> Regular expressions library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic"> Atomic operations library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread"> Thread support library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
</table></div>
<div></div>
</div></div>
</div>
<div class="t-navbar-sep"> </div>
<div class="t-navbar-head">
<a href="../thread.html" title="cpp/thread"> Thread support library</a><div class="t-navbar-menu"><div>
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h1"><td colspan="5"> Threads</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="thread.html" title="cpp/thread/thread"><span class="t-lines"><span>thread</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <code>this_thread</code> namespace</td></tr>
<tr class="t-nv-col-table">
<td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="get_id.html" title="cpp/thread/get id"><span class="t-lines"><span>get_id</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="yield.html" title="cpp/thread/yield"><span class="t-lines"><span>yield</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
</table></div></td>
<td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="sleep_for.html" title="cpp/thread/sleep for"><span class="t-lines"><span>sleep_for</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="sleep_until.html" title="cpp/thread/sleep until"><span class="t-lines"><span>sleep_until</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
</table></div></td>
</tr>
<tr class="t-nv-h1"><td colspan="5"> Mutual exclusion</td></tr>
<tr class="t-nv-col-table">
<td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="mutex.html" title="cpp/thread/mutex"><span class="t-lines"><span>mutex</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="timed_mutex.html" title="cpp/thread/timed mutex"><span class="t-lines"><span>timed_mutex</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="shared_mutex.html" title="cpp/thread/shared timed mutex"><span class="t-lines"><span>shared_timed_mutex</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx14">(C++14)</span></span></span></div>
</div></td></tr>
</table></div></td>
<td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="recursive_mutex.html" title="cpp/thread/recursive mutex"><span class="t-lines"><span>recursive_mutex</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="recursive_timed_mutex.html" title="cpp/thread/recursive timed mutex"><span class="t-lines"><span>recursive_timed_mutex</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
</table></div></td>
</tr>
<tr class="t-nv-h2"><td colspan="5"> Generic lock management</td></tr>
<tr class="t-nv-col-table">
<td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="lock_guard.html" title="cpp/thread/lock guard"><span class="t-lines"><span>lock_guard</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="unique_lock.html" title="cpp/thread/unique lock"><span class="t-lines"><span>unique_lock</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="shared_lock.html" title="cpp/thread/shared lock"><span class="t-lines"><span>shared_lock</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx14">(C++14)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="lock_tag_t.html" title="cpp/thread/lock tag t"><span class="t-lines"><span>defer_lock_t</span><span>try_to_lock_t</span><span>adopt_lock_t</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
</table></div></td>
<td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="lock.html" title="cpp/thread/lock"><span class="t-lines"><span>lock</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="try_lock.html" title="cpp/thread/try lock"><span class="t-lines"><span>try_lock</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="lock_tag.html" title="cpp/thread/lock tag"><span class="t-lines"><span>defer_lock</span><span>try_to_lock</span><span>adopt_lock</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="once_flag.html" title="cpp/thread/once flag"><span class="t-lines"><span>once_flag</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="call_once.html" title="cpp/thread/call once"><span class="t-lines"><span>call_once</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
</table></div></td>
</tr>
<tr class="t-nv-h1"><td colspan="5"> Condition variables</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="condition_variable.html" title="cpp/thread/condition variable"><span class="t-lines"><span>condition_variable</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="condition_variable_any.html" title="cpp/thread/condition variable any"><span class="t-lines"><span>condition_variable_any</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="notify_all_at_thread_exit.html" title="cpp/thread/notify all at thread exit"><span class="t-lines"><span>notify_all_at_thread_exit</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="cv_status/no_timeout.html" title="cpp/thread/cv status"><span class="t-lines"><span>cv_status</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> Futures</td></tr>
<tr class="t-nv-col-table">
<td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="promise.html" title="cpp/thread/promise"><span class="t-lines"><span>promise</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="future.html" title="cpp/thread/future"><span class="t-lines"><span>future</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="shared_future.html" title="cpp/thread/shared future"><span class="t-lines"><span>shared_future</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="packaged_task.html" title="cpp/thread/packaged task"><span class="t-lines"><span>packaged_task</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><strong class="selflink"><span class="t-lines"><span>async</span></span></strong></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
</table></div></td>
<td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="launch.html" title="cpp/thread/launch"><span class="t-lines"><span>launch</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="future_status.html" title="cpp/thread/future status"><span class="t-lines"><span>future_status</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="future_error.html" title="cpp/thread/future error"><span class="t-lines"><span>future_error</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="future_category.html" title="cpp/thread/future category"><span class="t-lines"><span>future_category</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table">
<div><a href="future_errc.html" title="cpp/thread/future errc"><span class="t-lines"><span>future_errc</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div></td></tr>
</table></div></td>
</tr>
</table></div>
<div></div>
</div></div>
</div>
<div class="t-navbar-sep"> </div>
</div>
<table class="t-dcl-begin"><tbody>
<tr class="t-dsc-header">
<td> <div>Defined in header <code><a href="../header/future.html" title="cpp/header/future">&lt;future&gt;</a></code>
 </div>
</td>
<td></td>
<td></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> Function, <span class="kw1">class</span>... <span class="me1">Args</span><span class="sy1">&gt;</span><br>
<p><a href="future.html"><span class="kw1813">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span><span class="kw1">typename</span> <a href="../types/result_of.html"><span class="kw526">std::<span class="me2">result_of</span></span></a><span class="sy1">&lt;</span>Function<span class="br0">(</span>Args...<span class="br0">)</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">type</span><span class="sy1">&gt;</span><br>
</p>
    async<span class="br0">(</span> Function<span class="sy3">&amp;&amp;</span> f, Args<span class="sy3">&amp;&amp;</span>... <span class="me1">args</span> <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td> (1) </td>
<td> <span class="t-mark-rev t-since-cxx11">(since C++11)</span> </td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> Function, <span class="kw1">class</span>... <span class="me1">Args</span> <span class="sy1">&gt;</span><br>
<p><a href="future.html"><span class="kw1813">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span><span class="kw1">typename</span> <a href="../types/result_of.html"><span class="kw526">std::<span class="me2">result_of</span></span></a><span class="sy1">&lt;</span>Function<span class="br0">(</span>Args...<span class="br0">)</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">type</span><span class="sy1">&gt;</span><br>
</p>
    async<span class="br0">(</span> <a href="launch.html"><span class="kw1817">std::<span class="me2">launch</span></span></a> policy, Function<span class="sy3">&amp;&amp;</span> f, Args<span class="sy3">&amp;&amp;</span>... <span class="me1">args</span> <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td> (2) </td>
<td> <span class="t-mark-rev t-since-cxx11">(since C++11)</span> </td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody></table>
<p>The template function <code>async</code> runs the function <code>f</code> asynchronously (potentially in a separate thread) and returns a <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> that will eventually hold the result of that function call.
</p>
<p>1) Behaves the same as <span class="t-c"><span class="mw-geshi cpp source-cpp">async<span class="br0">(</span><a href="launch.html"><span class="kw1818">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a> <span class="sy3">|</span> <a href="launch.html"><span class="kw1819">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">deferred</span></span></a>, f, args...<span class="br0">)</span></span></span>.  In other words, <code>f</code> may be executed in another thread or it may be run synchronously when the resulting <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> is queried for a value.
</p>
<p>2) Calls a function <code>f</code> with arguments <code>args</code> according to a specific launch policy <code>policy</code>: 
</p>
<ul><li> If the <i>async</i> flag is set (i.e. <span class="t-c"><span class="mw-geshi cpp source-cpp">policy <span class="sy3">&amp;</span> <a href="launch.html"><span class="kw1818">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a> <span class="sy3">!</span><span class="sy1">=</span> <span class="nu0">0</span></span></span>), then <code>async</code> executes the function <code>f</code> on a separate thread of execution as if spawned by <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="thread.html"><span class="kw1782">std::<span class="me2">thread</span></span></a><span class="br0">(</span>f, args...<span class="br0">)</span></span></span>, except that if the function <code>f</code> returns a value or throws an exception, it is stored in the shared state accessible through the <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> that <code>async</code> returns to the caller.
</li></ul>
<ul><li> If the <i>deferred</i> flag is set (i.e. <span class="t-c"><span class="mw-geshi cpp source-cpp">policy <span class="sy3">&amp;</span> <a href="launch.html"><span class="kw1819">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">deferred</span></span></a> <span class="sy3">!</span><span class="sy1">=</span> <span class="nu0">0</span></span></span>), then <code>async</code> converts <code>args...</code> the same way as by <span class="t-lc"><a href="thread.html" title="cpp/thread/thread">std::thread</a></span> constructor, but does not spawn a new thread of execution. Instead, <i>lazy evaluation</i> is performed: the first call to a non-timed wait function on the <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> that <code>async</code> returned to the caller will cause <code>f(args...)</code> to be executed in the current thread. The result or exception is placed in the shared state associated with the future and only then it is made ready. All further accesses to the same <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> will return the result immediately.
</li></ul>
<ul><li> If both the <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::async</a></span> and <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::deferred</a></span> flags are set in <code>policy</code>, it is up to the implementation whether to perform asynchronous execution or lazy evaluation.
</li></ul>
<ul><li> <span class="t-mark-rev t-since-cxx14">(C++14)</span> If neither <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::async</a></span> nor <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::deferred</a></span>, nor any implementation-defined policy flag is set in <code>policy</code>, the behavior is undefined.
</li></ul>
<p>In any case, the call to <code>std::async</code> <i>synchronizes-with</i> (as defined in <span class="t-lc"><a href="../atomic/memory_order.html" title="cpp/atomic/memory order">std::memory_order</a></span>) the call to <code>f</code>, and the completion of <code>f</code> is <i>sequenced-before</i> making the shared state ready. If the <code>async</code> policy is chosen, the associated thread completion <i>synchronizes-with</i> the successful return from the first function that is waiting on the shared state, or with the return of the last function that releases the shared state, whichever comes first.
</p>
<table id="toc" class="toc"><tr><td>
<div id="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1 tocsection-1"><a href="async.html%23Parameters"><span class="tocnumber">1</span> <span class="toctext">Parameters</span></a></li>
<li class="toclevel-1 tocsection-2"><a href="async.html%23Return_value"><span class="tocnumber">2</span> <span class="toctext">Return value</span></a></li>
<li class="toclevel-1 tocsection-3"><a href="async.html%23Exceptions"><span class="tocnumber">3</span> <span class="toctext">Exceptions</span></a></li>
<li class="toclevel-1 tocsection-4"><a href="async.html%23Notes"><span class="tocnumber">4</span> <span class="toctext">Notes</span></a></li>
<li class="toclevel-1 tocsection-5"><a href="async.html%23Example"><span class="tocnumber">5</span> <span class="toctext">Example</span></a></li>
</ul>
</td></tr></table>
<h3>
<span class="editsection">[<a href="http%3A//en.cppreference.com/mwiki/index.php" title="Edit section: Parameters">edit</a>]</span> <span class="mw-headline" id="Parameters">Parameters</span>
</h3>
<table class="t-par-begin">


<tr class="t-par">
<td>  f
</td>
<td> -
</td>
<td>  <a href="../concept/Callable.html" title="cpp/concept/Callable"><code>Callable</code></a> object to call
</td>
</tr>
<tr class="t-par">
<td>  args...
</td>
<td> -
</td>
<td>  parameters to pass to <code>f</code>
</td>
</tr>
<tr class="t-par">
<td>  policy
</td>
<td> -
</td>
<td>  bitmask value, where individual bits control the allowed methods of execution
<table class="t-dsc-begin">

<tr class="t-dsc-sep">
<td colspan="2">
</td>
</tr>
<tr class="t-dsc-hitem">
<td>  Bit
</td>
<td>  Explanation
</td>
</tr>


<tr class="t-dsc-sep">
<td colspan="2">
</td>
</tr>
<tr class="t-dsc">
<td>  <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::async</a></span>
</td>
<td>  enable asynchronous evaluation
</td>
</tr>


<tr class="t-dsc-sep">
<td colspan="2">
</td>
</tr>
<tr class="t-dsc">
<td>  <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::deferred</a></span>
</td>
<td>  enable lazy evaluation
</td>
</tr>

</table>
<p><br>
</p>
</td>
</tr>
</table>
<h3>
<span class="editsection">[<a href="http%3A//en.cppreference.com/mwiki/index.php" title="Edit section: Return value">edit</a>]</span> <span class="mw-headline" id="Return_value">Return value</span>
</h3>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="future.html"><span class="kw1813">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span>std<span class="sy4">::</span><span class="me2">result_of_t</span><span class="sy1">&lt;</span>std<span class="sy4">::</span><span class="me2">decay_t</span><span class="sy1">&lt;</span>F<span class="sy1">&gt;</span><span class="br0">(</span>std<span class="sy4">::</span><span class="me2">decay_t</span><span class="sy1">&lt;</span>Args<span class="sy1">&gt;</span>...<span class="br0">)</span><span class="sy1">&gt;&gt;</span></span></span> referring to the shared state created by this call to <code>std::async</code>. 
</p>
<h3>
<span class="editsection">[<a href="http%3A//en.cppreference.com/mwiki/index.php" title="Edit section: Exceptions">edit</a>]</span> <span class="mw-headline" id="Exceptions">Exceptions</span>
</h3>
<p>Throws <span class="t-lc"><a href="../error/system_error.html" title="cpp/error/system error">std::system_error</a></span> with error condition <span class="t-lc"><a href="../error/errc.html" title="cpp/error/errc">std::errc::resource_unavailable_try_again</a></span> if the launch policy equals <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::async</a></span> and the implementation is unable to start a new thread (if the policy is <code>async|deferred</code> or has additional bits set, it will fall back to deferred or the implementation-defined policies in this case)
</p>
<h3>
<span class="editsection">[<a href="http%3A//en.cppreference.com/mwiki/index.php" title="Edit section: Notes">edit</a>]</span> <span class="mw-headline" id="Notes">Notes</span>
</h3>
<p>The implementation may extend the behavior of the first overload of <span class="t-lc"><strong class="selflink">std::async</strong></span> by enabling additional (implementation-defined) bits in the default launch policy.
</p>
<p>If the <code>std::future</code> obtained from  <code>std::async</code> has temporary object lifetime (not moved or bound to a variable), the destructor of the <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> will block at the end of the full expression until the asynchronous operation completes, essentially code such as the following synchronous:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1">std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><a href="launch.html"><span class="kw1818">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a>, <span class="br0">[</span><span class="br0">]</span><span class="br0">{</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// temporary's dtor waits for f()</span>
std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><a href="launch.html"><span class="kw1818">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a>, <span class="br0">[</span><span class="br0">]</span><span class="br0">{</span> g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// does not start until f() completes</span></pre></div></div>
<h3>
<span class="editsection">[<a href="http%3A//en.cppreference.com/mwiki/index.php" title="Edit section: Example">edit</a>]</span> <span class="mw-headline" id="Example">Example</span>
</h3>
<div class="t-example">
<div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">Run this code</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;vector&gt;</span>
<span class="co2">#include &lt;algorithm&gt;</span>
<span class="co2">#include &lt;numeric&gt;</span>
<span class="co2">#include &lt;future&gt;</span>
 
<span class="kw1">template</span> <span class="sy1">&lt;</span><span class="kw1">typename</span> RAIter<span class="sy1">&gt;</span>
<span class="kw4">int</span> parallel_sum<span class="br0">(</span>RAIter beg, RAIter end<span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw1">typename</span> RAIter<span class="sy4">::</span><span class="me2">difference_type</span> len <span class="sy1">=</span> end<span class="sy2">-</span>beg<span class="sy4">;</span>
    <span class="kw1">if</span><span class="br0">(</span>len <span class="sy1">&lt;</span> <span class="nu0">1000</span><span class="br0">)</span>
        <span class="kw1">return</span> <a href="../algorithm/accumulate.html"><span class="kw1345">std::<span class="me2">accumulate</span></span></a><span class="br0">(</span>beg, end, <span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span>
 
    RAIter mid <span class="sy1">=</span> beg <span class="sy2">+</span> len<span class="sy2">/</span><span class="nu0">2</span><span class="sy4">;</span>
    <span class="kw4">auto</span> handle <span class="sy1">=</span> std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><a href="launch.html"><span class="kw1818">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a>,
                              parallel_sum<span class="sy1">&lt;</span>RAIter<span class="sy1">&gt;</span>, mid, end<span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">int</span> sum <span class="sy1">=</span> parallel_sum<span class="br0">(</span>beg, mid<span class="br0">)</span><span class="sy4">;</span>
    <span class="kw1">return</span> sum <span class="sy2">+</span> handle.<span class="me1">get</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../container/vector.html"><span class="kw1067">std::<span class="me2">vector</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v<span class="br0">(</span><span class="nu0">10000</span>, <span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
    <a href="../io/basic_ostream.html"><span class="kw1439">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"The sum is "</span> <span class="sy1">&lt;&lt;</span> parallel_sum<span class="br0">(</span>v.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span>, v.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>Output:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">The sum is 10000</pre></div></div> 
</div>

<!-- 
NewPP limit report
Preprocessor visited node count: 3876/1000000
Preprocessor generated node count: 6878/1000000
Post‐expand include size: 110921/2097152 bytes
Template argument size: 22813/2097152 bytes
Highest expansion depth: 16/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_en_:pcache:idhash:2516-0!*!0!!en!*!* and timestamp 20140414124904 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    Retrieved from "<a href="http%3A//en.cppreference.com/mwiki/index.php">http://en.cppreference.com/mwiki/index.php?title=cpp/thread/async&amp;oldid=69576</a>"                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <div id="catlinks" class="catlinks catlinks-allhidden"></div>                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.086 secs. -->
	</body>
<!-- Cached 20140414124904 -->
</html>
